package alioss

import (
	"io"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

type base struct {
	c *oss.Client
}

func NewOss() (*base, error) {
	// 创建OSSClient实例。
	client, err := oss.New(os.Getenv("OSS_ENDPOINT"), os.Getenv("ALI_KEY_ID"), os.Getenv("ALI_KEY_SECRET"))
	return &base{c: client}, err
}

func (s *base) newBucket() (*oss.Bucket, error) {
	return s.c.Bucket(os.Getenv("OSS_BUCKET"))
}
func (s *base) UploadFile(filePath string, file io.Reader, contentType string) (path string, err error) {
	b, err := s.newBucket()
	if err != nil {
		return "", err
	}
	// 上传至oss
	err = b.PutObject(filePath, file, oss.ContentType(contentType))
	if err != nil {
		return "", err
	}
	return filePath, nil
}
